﻿using CCWin;
using DC.Utils;
using System;
using System.IO;
using System.Windows.Forms;

namespace DC.Views
{

    public partial class Form_ProcessListEdit : Skin_DevExpress
    {
        public int ListType = 0;//1为加工类型 2为色号 3为加工单位
        public Form_ProcessListEdit()
        {
            InitializeComponent();
        }

        private void Form_ProcessListEdit_Load(object sender, EventArgs e)
        {
            String FilePath = string.Empty;
            int i = 0;
            switch (ListType)
            {

                case 1:
                    this.Text = "加工类型";
                    FilePath = Application.StartupPath + "\\config\\ProcessType.bin";
                    if (!File.Exists(FilePath))
                    {
                        File.Create(FilePath);
                    }
                    i = 0;
                    foreach (string item in FileHelper.ReadFile(FilePath).Split('\n'))
                    {
                        Plists.Rows.Add();
                        Plists.Rows[i].Cells[0].Value = item;
                        i++;
                    }
                    break;
                case 2:
                    this.Text = "色号";
                    FilePath = Application.StartupPath + "\\config\\ProcessColor.bin";
                    if (!File.Exists(FilePath))
                    {
                        File.Create(FilePath);
                    }
                    i = 0;
                    foreach (string item in FileHelper.ReadFile(FilePath).Split('\n'))
                    {
                        Plists.Rows.Add();
                        Plists.Rows[i].Cells[0].Value = item;
                        i++;
                    }
                    break;
                case 3:
                    this.Text = "加工单位";
                    FilePath = Application.StartupPath + "\\config\\ProcessFirm.bin";
                    if (!File.Exists(FilePath))
                    {
                        File.Create(FilePath);
                    }
                    i = 0;
                    foreach (string item in FileHelper.ReadFile(FilePath).Split('\n'))
                    {
                        Plists.Rows.Add();
                        Plists.Rows[i].Cells[0].Value = item;
                        i++;
                    }
                    break;
                default:
                    MessageBox.Show("加载出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }


        }

        private void Save_button_Click(object sender, EventArgs e)
        {
            String FilePath = string.Empty;
            switch (ListType)
            {
                case 1:
                    string TypeList = string.Empty;
                    FilePath = Application.StartupPath + "\\config\\ProcessType.bin";
                    for (int i = 0; i < Plists.Rows.Count - 1; i++)
                    {
                        TypeList += Plists.Rows[i].Cells[0].Value.ToString() + ((i == Plists.Rows.Count - 2) ? string.Empty : "\n");
                    }
                    FileHelper.WriteFile(FilePath, TypeList);
                    MessageBox.Show("保存成功", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    break;
                case 2:
                    string ColorList = string.Empty;
                    FilePath = Application.StartupPath + "\\config\\ProcessColor.bin";
                    for (int i = 0; i < Plists.Rows.Count - 1; i++)
                    {
                        ColorList += Plists.Rows[i].Cells[0].Value.ToString() + ((i == Plists.Rows.Count - 2) ? string.Empty : "\n");
                    }
                    FileHelper.WriteFile(FilePath, ColorList);
                    MessageBox.Show("保存成功", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    break;
                case 3:
                    string FirmList = string.Empty;
                    FilePath = Application.StartupPath + "\\config\\ProcessFirm.bin";
                    for (int i = 0; i < Plists.Rows.Count - 1; i++)
                    {
                        FirmList += Plists.Rows[i].Cells[0].Value.ToString() + ((i == Plists.Rows.Count - 2) ? string.Empty : "\n");
                    }
                    FileHelper.WriteFile(FilePath, FirmList);
                    MessageBox.Show("保存成功", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    break;
                default:
                    break;
            }
        }
    }
}
